package cn.qn.service.impl.news;

import cn.qn.base.PageVO;
import cn.qn.dto.news.ImageTextDTO;
import cn.qn.dto.news.ImageTextQueryDTO;
import cn.qn.enums.BooleanEnum;
import cn.qn.exceptions.BizException;
import cn.qn.mapper.news.ImageTextMapper;
import cn.qn.moudle.news.ImageText;
import cn.qn.repository.news.ImageTextRepository;
import cn.qn.service.news.ImageTextService;
import cn.qn.utils.DateUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: Bryant
 * @date: 2019/6/4
 * @description:
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = ImageTextService.class)
public class ImageTextServiceImpl implements ImageTextService {

    @Autowired
    private ImageTextRepository imageTextRepository;
    @Resource
    private ImageTextMapper imageTextMapper;
    @Value("${aliyun.oss.showUrl}")
    private String showUrl;
    @Override
    public PageVO<ImageTextDTO> pageQueryImageTexts(ImageTextQueryDTO query) {
        PageBounds pb = new PageBounds(query.getPageBounds().getPage(), query.getPageBounds().getLimit());
        List<ImageTextDTO> list = imageTextMapper.pageQueryImageTexts(query, pb);
        Integer count = imageTextMapper.pageQueryImageTextsCount(query);
        return PageVO.convert(pb.getPage(), pb.getLimit(), list, count);
    }

    @Override
    public ImageTextDTO queryDetailById(Integer id) {
        ImageTextDTO dto = new ImageTextDTO();
        ImageText imageText = imageTextRepository.findImageTextById(id);
        BeanUtils.copyProperties(imageText, dto);
        if(StringUtils.isNotEmpty(dto.getBannerImage())){
            dto.setShowBannerImage(showUrl+dto.getBannerImage());
        }
        if(StringUtils.isNotEmpty(dto.getCoverImage())){
            dto.setShowCoverImage(showUrl+dto.getCoverImage());
        }
        dto.setPublishTime(DateFormatUtils.format(imageText.getPublishTime(), "yyyy-MM-dd HH:mm:ss"));
        dto.setCreateTime(DateFormatUtils.format(imageText.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        return dto;
    }

    @Override
    public void addImageText(ImageTextDTO dto) throws BizException {
        ImageText imageText = new ImageText();
        BeanUtils.copyProperties(dto, imageText);
        imageText.setPublishTime(DateUtils.parseStrToDate(dto.getPublishTime(), DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        imageText.setReadNum(0);
        imageText.setCreateBy(dto.getCreateBy());
        imageText.setCreateTime(new Date());
        imageText.setStatus(BooleanEnum.TRUE.getCode().toString());
        imageTextRepository.save(imageText);
    }

    @Override
    public void editImageText(ImageTextDTO dto) throws BizException {
        ImageText imageText = imageTextRepository.findImageTextById(dto.getId());
        if (imageText == null) {
            throw new BizException("该记录不存在");
        }
        BeanUtils.copyProperties(dto, imageText);
        imageText.setPublishTime(DateUtils.parseStrToDate(dto.getPublishTime(), DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        imageText.setUpdateBy(dto.getUpdateBy());
        imageText.setUpdateTime(new Date());
        imageTextRepository.save(imageText);
    }

    @Override
    public void editStatus(ImageTextDTO dto) throws BizException {
        ImageText imageText = imageTextRepository.findImageTextById(dto.getId());
        if (imageText == null) {
            throw new BizException("该记录不存在");
        }
        imageText.setStatus(dto.getStatus());
        imageText.setUpdateBy(dto.getUpdateBy());
        imageText.setUpdateTime(new Date());
        imageTextRepository.save(imageText);
    }

    @Override
    public void deleteImageText(Integer id) {
        imageTextRepository.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ImageText topStatus(Integer id, String updateName) {
        ImageText top = imageTextRepository.findByTop(Boolean.TRUE);
        if(null != top){
            top.setTop(Boolean.FALSE);
            top.setUpdateBy(updateName);
            imageTextRepository.save(top);
        }
        ImageText imageText = imageTextRepository.findImageTextById(id);
        if(null != imageText){
            imageText.setTop(Boolean.TRUE);
            imageText.setUpdateBy(updateName);
            imageTextRepository.save(imageText);
        }
        return imageText;
    }

    @Override
    public ImageTextDTO getTop() {
        ImageText top = imageTextRepository.findByTop(Boolean.TRUE);
        if(null != top){
            return queryDetailById(top.getId());
        }else{
            ImageText first = imageTextRepository.findFirstByOrderByCreateTimeDesc();
            if(null == first){
                return null;
            }
            return queryDetailById(first.getId());
        }
    }
}
